package com.cqtczh.qhd.driver.acticity.carteam;

import httptools.HttpJsonTool;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javabeen.CarTeamIdBeen;
import javabeen.CarTeamInfoBeen;
import javabeen.CompleteCarTeamInfoBeen;
import javabeen.UserInfoBeen;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tools.L;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.api.GagApi;
import com.cqtczh.qhd.driver.model.CarTeam;
import com.cqtczh.qhd.driver.vo.JsonRequest;
import com.cqtczh.qhd.driver.vo.JsonResponse;
import com.cqtczh.qhd.driver.vo.State;

public class CarTeamConter extends BaseActivity implements OnClickListener {

	private AppContent app;
	private PreferencesCookieStore preferencesCookieStore;
	private String userId;
	private List<CarTeamInfoBeen> team;
	private List<CarTeamIdBeen> Jionteam;
	private  List<CompleteCarTeamInfoBeen> CompleteCarTeamInfoBeen;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_team_conter_v2);
		Ini();
	}

	private void Ini() {

		View CreatCarTeam = findViewById(R.id.CreatCarTeam);
		View MyCarTeam = findViewById(R.id.MyCreatCarTeam);
		View MyJionCarTeam = findViewById(R.id.MyJionCarTeam);
		View CompleteCarTeam= findViewById(R.id.CompleteCarTeam);
		View CreatCarTeam_2=findViewById(R.id.CreatCarTeam_2);
		CreatCarTeam_2.setOnClickListener(this);
		CompleteCarTeam.setOnClickListener(this);
		CreatCarTeam.setOnClickListener(this);
		MyCarTeam.setOnClickListener(this);
		MyJionCarTeam.setOnClickListener(this);
		TextView textView = (TextView) findViewById(R.id.head_title);
		textView.setText("车队管理");
		app = (AppContent) getApplication();
		userId = app.userId;
	}

	private void GetMyCreatCarTeam() {
		
		 app=(AppContent) getApplication();
		 UserInfoBeen userInfoBeen=app.userInfoBeen;
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		Map<String, String> data=new HashMap<String, String>();
		
		data.put("leaderid", userInfoBeen.getCarownerid()+"");
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what==1) {
					 List<CarTeamIdBeen>datamap=new ArrayList<CarTeamIdBeen>();
					datamap=(List<CarTeamIdBeen>) msg.obj;
					if (datamap!=null&&datamap.size()>0) {
						Intent intent=new Intent(CarTeamConter.this,MyCarTeamMember.class);
						intent.putExtra("teamid",datamap.get(0).getMotorcadeid()+"");  //motorcadeid
						intent.putExtra("carteamidbeen", datamap.get(0));
					    startActivity(intent);
						//Toast.makeText(CarTeamConter.this, (String)msg.obj, Toast.LENGTH_LONG);
					}
				}else {
					Toast.makeText(CarTeamConter.this, (String) msg.obj, Toast.LENGTH_LONG).show();
				}
			}
		});
		//TypeToken<List<CarTeamInfoBeen>>() {}.getType();
		Type type=new TypeToken<List<CarTeamIdBeen>>(){}.getType();
		httpJsonTool.StartWork(data, GagApi.GET_MY_CREAT_CAR_TEAM, type, null);
	}

	private void GetJoinCarTeam() {
		app = (AppContent) getApplication();
		UserInfoBeen userInfoBeen=app.userInfoBeen;
		Type type=new TypeToken<List<CarTeamIdBeen>>(){}.getType();
		Map<String, Object> dataMap=new HashMap<String, Object>();
		dataMap.put("carownerid", userInfoBeen.getCarownerid()+"");
		HttpJsonTool httpJsonTool=new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (msg.what==1) {
					Jionteam=(List<CarTeamIdBeen>) msg.obj;
					Intent intent=new Intent(CarTeamConter.this,JionCarTeamInfo.class);
					intent.putExtra("jioncarteaminfo", Jionteam.get(0));
					intent.putExtra("leaderid", Jionteam.get(0).getLeaderid()+"");
					startActivity(intent);
				}else {
					
					Toast.makeText(CarTeamConter.this, (String)msg.obj, Toast.LENGTH_LONG).show();
				}
			
			}
		});
		httpJsonTool.StartWork(dataMap, GagApi.GET_JOIN_CAR_TEAM, type, null);
	}

	private void GetCompleteCarTeamInfo(){
		HttpJsonTool httpJsonTool=new HttpJsonTool(this);
		HashMap< String, String> data=new HashMap<String, String>();
	 new ArrayList<CompleteCarTeamInfo>();
		
		data.put("carownerid", app.userInfoBeen.getCarownerid()+"");
		httpJsonTool.SetOnLister(new Handler(){
			
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what==1) {
					CompleteCarTeamInfoBeen=(List<CompleteCarTeamInfoBeen>) msg.obj;
					if (CompleteCarTeamInfoBeen.size()>0) {
						Intent intent=new Intent(CarTeamConter.this,CompleteCarTeamInfo.class);
						intent.putExtra("CompleteCarTeamInfo", CompleteCarTeamInfoBeen.get(0).getOrgid()+"");
						intent.putExtra("completeinfobeen", CompleteCarTeamInfoBeen.get(0));
						startActivity(intent);
					}
				}else {
					Toast.makeText(CarTeamConter.this, (String)msg.obj, Toast.LENGTH_LONG).show();
				}
			}
			
		});
		Type type=new TypeToken<List<CompleteCarTeamInfoBeen>>(){}.getType();
		httpJsonTool.StartWork(data, GagApi.GET_COMPLETE_CAR_TEAM_INFO, type, null);
		
	}
	
	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.CreatCarTeam:
			Intent intent = new Intent(CarTeamConter.this,
					Activity_Creat_Car_Team.class);
			startActivity(intent);
			break;
		case R.id.MyCreatCarTeam:
			GetMyCreatCarTeam();
			break;
		case R.id.MyJionCarTeam:
			GetJoinCarTeam();
			break;
		case R.id.CompleteCarTeam:
			GetCompleteCarTeamInfo();
			break;
		case R.id.CreatCarTeam_2:
			Intent intent2 = new Intent(CarTeamConter.this,
					Activity_Creat_Car_Team.class);
			startActivity(intent2);
		default:
			break;
		}
	}
}
